<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>

    *{
      margin: 0;
      padding: 0;
    }
    .box{
      position: fixed;
      bottom: 0;
      display: flex;
      justify-content: space-around;
      width: 100%;
    }
    .item{
      width: 25%;
      background-color: grey; 
      text-align: center;
      height: 40px;
      line-height: 40px;
      font-size: 18px;
    }

    .active{
      color: red;
    }

  </style>
</head>
<body>
  
  <p class="text" style="text-align: center; font-size: 40px;">主页</p>

  <div class="box">
    <p class="item" data-content="主页">主页</p>
    <p class="item" data-content="发现">发现</p>
    <p class="item" data-content="订单">订单</p>
    <p class="item" data-content="我的">我的</p>
  </div>

  <script>

    var box = document.querySelector(".box");
    var items = document.querySelectorAll(".item");
    var p = document.querySelector(".text");

    box.addEventListener("click", function(e){
      if(e.target.tagName == "P"){
        for(let i = 0; i < items.length; i++){
          items[i].classList.remove("active");
        }
        e.target.classList.add("active");

        var content = e.target.dataset.content;
        // p.innerText = e.target.dataset.content;
        var path = e.target.dataset.content;
        p.textContent = content;
        p.style.color = "#" + Math.random().toString(16).substr(2, 6);
        history.pushState({ to : path, content : content }, "标题~" , path);
      }
      console.log(history.state);
    })

    window.addEventListener("popstate", function(){

      console.log(history.state.to);

      for(let i = 0; i < items.length; i++){
        if(items[i].dataset.content == history.state.to){
          items[i].classList.add("active");
          p.textContent = items[i].dataset.content;
          p.style.color = "#" + Math.random().toString(16).substr(2, 6);
        }else{
          items[i].classList.remove("active");
        }
      }

    })

  </script>

</body>
</html>